﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_BCJDCJDataInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public String strHtml = "";

        Literal Literal1;

        protected override void OnLoad(EventArgs e)
        {
            if (!base.Page.IsPostBack)
            {
                Literal1 = (Literal)FindControl("Literal1");
                string tjlx = Page.Request["tjlx"];
                string jaqk = Page.Request["jaqk"];
                //如果统计类型为1：拆迁项目裁决数据统计表（含比例）
                if (tjlx == "1")
                {
                    CreateHtml();
                }
                //如果统计类别为0：拆迁项目裁决数据统计表
                else if (tjlx == "0")
                {
                    CreatHtmlTable cTable = new CreatHtmlTable();
                    StructHtmlTable sHtable = new StructHtmlTable();
                    Hashtable htHidden = new Hashtable();
                    htHidden = HiddenField(jaqk);
                    string CountStr = "征收户数,按转户数,申请裁决户数,正在审理,调解解决,不予受理,已经裁决户数,中止裁决户数,终结裁决户数,催告已发出,催告已送达,已提交法院户数,已听证户数,准予申请户数,准予撤回申请户数,不予申请户数,已调节,已强制,先予已提交法院户数,先予已听证户数,先予准予申请户数,先予准予撤回申请户数,先予不予申请户数,先予已调节,先予已强制";

                    sHtable.DataTableTemp = GetData();
                    sHtable.PageSize = 20;
                    sHtable.StatField = CountStr;//绘制表格的所有列字段名称
                    sHtable.TableCss = "window_tab_list";
                    sHtable.HiddenField = htHidden;
                    sHtable.TableName = "征收补偿决定裁决数据统计表";
                    strHtml = cTable.CreateHtml(sHtable);
                }
                //索引为2:拆迁项目裁决受理情况表
                else
                {
                    CreateHtmlCjsl();
                }
                Literal1.Text = strHtml;
            }

        }

        private void CreateHtml()
        {
            DataTable dt = GetData();
            System.Text.StringBuilder sb_Content = new System.Text.StringBuilder();
            int cqhs = 0;//征收户数
            int sqcjhs = 0;//申请裁决户数
            int zzslhs = 0;//正在审理户数
            int tjhs = 0;//调解户数
            int byslhs = 0;//不予受理户数
            int cjhs = 0;//裁决户数
            int azhs = 0;//按转户数
            double sqScq = 0.00;
            double cjScq = 0.00;
            double cjSsq = 0.00;
            double tjSsq = 0.00;

            System.Text.StringBuilder sb_html = new System.Text.StringBuilder();
            string strPages = string.Empty;// "<div class='PageNext'></div>";
            int CountStr = 1;

            int loop = 0; //要生成的表格数

            int rows = 12;  //每个表格的行数

            int mod = 0;  //余数
            int start = 0;
            int end = 0;
            string s = string.Empty;
            if (dt != null)
            {
                loop = dt.Rows.Count / rows;
                mod = dt.Rows.Count % rows;

                if (mod != 0)
                {
                    loop = loop + 1;
                }
            }
            string strHead = string.Empty;
            string pagebreak = "page-break-after:always;";
            string strEnd = "</table><br>";
            for (int loops = 0; loops < loop; loops++)
            {
                if (loops == loop - 1)
                {
                    pagebreak = "";
                }
                strHead = string.Format(@"<table border='1'  cellpadding='0' cellspacing='0' width='97%' class='tab' style='{0}'>
                <th colspan='15'>拆迁项目裁决数据统计表</th>
                <tr>
                <td>序号</td>
                <td>许可证号</td>
                <td>单位名称</td>
                <td>项目名称</td>
                <td>征收户数</td>
                <td>按转户数</td>
                <td>申请裁决</td>
                <td>正在审理</td>
                <td>调解户数</td>
                <td>不予受理</td>
                <td>裁决户数</td>                
                <td>申请户数/征收户数%</td>
                <td>裁决户数/征收户数%</td>
                <td>裁决户数/申请户数%</td>
                <td>调解户数/申请户数%</td>
                </tr>", pagebreak);
                start = loops * rows;
                end = start + rows;
                #region
                for (int m = start; m < end && m < dt.Rows.Count; m++)
                {
                    sb_Content.AppendFormat("<td style='font-size: 12px;'>{0}</td>", CountStr);
                    CountStr++;
                    foreach (DataColumn cl in dt.Columns)
                    {
                        if (dt.Rows[m][cl].ToString().Trim() != "")
                        {
                            switch (cl.ToString())
                            {
                                default:
                                    sb_Content.AppendFormat("<td style='font-size: 12px;'>{0}</td>", dt.Rows[m][cl].ToString());
                                    break;
                            }
                        }
                        else
                        {
                            sb_Content.AppendFormat("<td>{0}</td>", "&nbsp;");
                        }
                    }
                    int cCqhs = 0;
                    if (dt.Rows[m]["征收户数"].ToString() != "")
                    {
                        cCqhs = int.Parse(dt.Rows[m]["征收户数"].ToString().Trim());
                    }
                    int cCjhs = int.Parse(dt.Rows[m]["裁决户数"].ToString().Trim());
                    int cSqcjhs = int.Parse(dt.Rows[m]["申请裁决户数"].ToString().Trim());
                    int cTjhs = int.Parse(dt.Rows[m]["调解解决"].ToString().Trim());

                    if (cCqhs != 0)
                    {
                        sqScq = Convert.ToDouble(cSqcjhs) / Convert.ToDouble(cCqhs);
                        cjScq = Convert.ToDouble(cCjhs) / Convert.ToDouble(cCqhs);
                    }
                    if (cSqcjhs != 0)
                    {
                        cjSsq = Convert.ToDouble(cCjhs) / Convert.ToDouble(cSqcjhs);
                        tjSsq = Convert.ToDouble(cTjhs) / Convert.ToDouble(cSqcjhs);
                    }

                    sb_Content.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td>", doubleToPercent(sqScq), doubleToPercent(cjScq), doubleToPercent(cjSsq), doubleToPercent(tjSsq));
                    if (dt.Rows[m]["征收户数"].ToString() != "")
                    {
                        cqhs += int.Parse(dt.Rows[m]["征收户数"].ToString().Trim());
                    }
                    sqcjhs += int.Parse(dt.Rows[m]["申请裁决户数"].ToString().Trim());
                    zzslhs += int.Parse(dt.Rows[m]["正在审理"].ToString().Trim());
                    tjhs += int.Parse(dt.Rows[m]["调解解决"].ToString().Trim());
                    byslhs += int.Parse(dt.Rows[m]["不予受理"].ToString().Trim());
                    cjhs += int.Parse(dt.Rows[m]["裁决户数"].ToString().Trim());
                    azhs += int.Parse(dt.Rows[m]["按转户数"].ToString().Trim());
                    sb_Content.Append("</tr>");
                }
                #endregion

                if (cqhs != 0)
                {
                    sqScq = Convert.ToDouble(sqcjhs) / Convert.ToDouble(cqhs);
                    cjScq = Convert.ToDouble(cjhs) / Convert.ToDouble(cqhs);
                }
                if (sqcjhs != 0)
                {
                    cjSsq = Convert.ToDouble(cjhs) / Convert.ToDouble(sqcjhs);
                    tjSsq = Convert.ToDouble(tjhs) / Convert.ToDouble(sqcjhs);
                }

                if (loops == loop - 1)
                {
                    sb_Content.AppendFormat("<tr style='color:#ff0000'><td colspan='4'>总计：{10}</td><td>{0}</td><td>{11}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td><td>{9}</td></tr>",
                    cqhs, sqcjhs, zzslhs, tjhs, byslhs, cjhs, doubleToPercent(sqScq), doubleToPercent(cjScq), doubleToPercent(cjSsq), doubleToPercent(tjSsq), dt.Rows.Count + "条", azhs);
                }
                sb_html.AppendFormat("{0}{1}{2}{3}", strHead, sb_Content.ToString(), strEnd, strPages);
                sb_Content.Length = 0;
            }
            if (dt.Rows.Count == 0)
            {
                sb_Content.Append("<tr><td colspan='13' style='color:#ff0000'>没有找到相关信息！</td></tr>");
                sb_html.AppendFormat("{0}{1}{2}{3}", strHead, sb_Content.ToString(), strEnd, strPages);
            }
            strHtml = sb_html.ToString();
        }

        private void CreateHtmlCjsl()
        {
            DataTable dt = GetData();
            System.Text.StringBuilder sb_Content = new System.Text.StringBuilder();
            System.Text.StringBuilder sb_html = new System.Text.StringBuilder();
            string strPages = string.Empty;// "<div class='PageNext'></div>";
            int CountStr = 1;

            int loop = 0; //要生成的表格数

            int rows = 11;  //每个表格的行数

            int mod = 0;  //余数
            int start = 0;
            int end = 0;
            string s = string.Empty;
            if (dt != null)
            {
                loop = dt.Rows.Count / rows;
                mod = dt.Rows.Count % rows;

                if (mod != 0)
                {
                    loop = loop + 1;
                }
            }
            string strHead = string.Empty;
            string pagebreak = "page-break-after:always;";
            string strEnd = "</table><br>";
            for (int loops = 0; loops < loop; loops++)
            {
                if (loops == loop - 1)
                {
                    pagebreak = "";
                }
                strHead = string.Format(@"<table border='1'  cellpadding='0' cellspacing='0' width='97%' class='tab' style='{0}'>
                <th colspan='15'>拆迁项目裁决受理情况表</th>
                <tr>
                <td style='width:3%'>序号</td>
                <td style='width:6%'>许可证号</td>                
                <td style='width:18%'>项目名称</td>
                <td style='width:14%'>建设单位</td>
                <td style='width:6%'>项目地址</td>
                <td style='width:19%'>实施单位</td>                
                <td style='width:8%'>裁决受理时间</td>
                <td style='width:8%'>裁决听证时间</td>
                <td style='width:7%'>是否达70%</td>
                <td style='width:8%'>是否特事特办</td>
                <td style='width:4%'>备注</td> 
                </tr>", pagebreak);
                start = loops * rows;
                end = start + rows;
                #region
                for (int m = start; m < end && m < dt.Rows.Count; m++)
                {
                    sb_Content.AppendFormat("<td style='font-size: 12px;'>{0}</td>", CountStr);
                    CountStr++;
                    foreach (DataColumn cl in dt.Columns)
                    {
                        if (dt.Rows[m][cl].ToString().Trim() != "")
                        {
                            switch (cl.ToString())
                            {
                                case "最小时间":
                                    sb_Content.AppendFormat("<td style='font-size:12px;'>{0}</td>", string.Format("{0:d}", dt.Rows[m][cl]));
                                    break;
                                default:
                                    sb_Content.AppendFormat("<td style='font-size: 12px;'>{0}</td>", dt.Rows[m][cl].ToString());
                                    break;
                            }
                        }
                        else
                        {
                            sb_Content.AppendFormat("<td>{0}</td>", "&nbsp;");
                        }
                    }
                    sb_Content.Append("<td></td><td></td><td></td><td></td>");
                    sb_Content.Append("</tr>");
                }
                #endregion

                if (loops == loop - 1)
                {
                    sb_Content.AppendFormat(@"<tr style='color:#ff0000'><td>总计</td><td colspan='10'>{0}条记录</td></tr>", CountStr - 1);
                }
                sb_html.AppendFormat("{0}{1}{2}{3}", strHead, sb_Content.ToString(), strEnd, strPages);
                sb_Content.Length = 0;
            }
            if (dt.Rows.Count == 0)
            {
                sb_Content.Append("<tr><td colspan='13' style='color:#ff0000'>没有找到相关信息！</td></tr>");
                sb_html.AppendFormat("{0}{1}{2}{3}", strHead, sb_Content.ToString(), strEnd, strPages);
            }
            strHtml = sb_html.ToString();
        }

        /// <summary>
        /// 获得统计的数据返回DataTable
        /// </summary>
        /// <returns></returns>
        protected DataTable GetData()
        {
            //string whereValue = Page.Session["whereValue"].ToString();
            string whereValue = Page.Request["wherekey"];
            string tjlx = Page.Request["tjlx"];
            string fsqk = Page.Request["fsqk"];
            string strDate = Page.Request["strdata"];
            //string strDate = Page.Session["strDate"].ToString();
            //CqcjInfo cqcj = new CqcjInfo();
            //DataTable dtTemp = new DataTable();
            //if (tjlx != "2")
            //{
            //    dtTemp = cqcj.GetCjsjStat(whereValue, fsqk, strDate);
            //}
            //else
            //{
            //    dtTemp = cqcj.GetCjslInfoTable(whereValue, fsqk, strDate);
            //}
            DataTable dtTemp = new DataTable();
            if (tjlx != "2")
            //如果统计类型 不为拆迁项目裁决受理情况表 0  1 拆迁项目裁决数据统计表（含比例）、拆迁项目裁决数据统计表
            {
                dtTemp = _EluZSDataProvider.GetCjsjStat(whereValue, fsqk, strDate);
            }
            //如果统计类型为 2   拆迁项目裁决受理情况表
            else
            {
                dtTemp = _EluZSDataProvider.GetCjslInfoTable(whereValue, fsqk, strDate);
            }
            //dtTemp = _EluZSDataProvider.GetCjsjStat(whereValue, fsqk, strDate);
            return dtTemp;
        }

        /// <summary>
        /// 小数转换为百分数
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        protected string doubleToPercent(double d)
        {
            string s = "0%";
            if (!string.IsNullOrEmpty(Convert.ToString(d)))
            {
                s = string.Format("{0:P}", d);
            }
            return s;
        }

        /// <summary>
        /// 根据结案情况判断隐藏域中应该为何值
        /// </summary>
        /// <param name="jaqk">结案情况</param>
        /// <returns></returns>
        protected Hashtable HiddenField(string jaqk)
        {
            Hashtable htHidden = new Hashtable();
            if (jaqk == "已经裁决及不予受理")
            {
                htHidden.Add("正在审理", "");
                htHidden.Add("调解解决", "");
            }
            else if (jaqk == "正在审理")
            {
                htHidden.Add("不予受理", "");
                htHidden.Add("调解解决", "");
                htHidden.Add("裁决户数", "");
            }
            else if (jaqk == "不予受理")
            {
                htHidden.Add("正在审理", "");
                htHidden.Add("调解解决", "");
                htHidden.Add("裁决户数", "");

            }
            else if (jaqk == "调解解决")
            {
                htHidden.Add("正在审理", "");
                htHidden.Add("不予受理", "");
                htHidden.Add("裁决户数", "");
            }
            else
            {

            }
            return htHidden;
        }


    }





    
}


